#!/user/bin/even Python3
# -*- coding:utf-8 -*-
# GetUrlDatas.py
# 获取url返回数据
# author:zhaohexin
# time：2020/7/29 15:52

import requests
import json

from conf import SendHistory


class GetUrlDatas:
    def __init__(self, url_condition, mywindow, progressBarValue):
        self.url = SendHistory.search_url
        self.url_condition = url_condition
        self.mywindow = mywindow
        self.progressBarValue = progressBarValue

    def get_datas(self):
        """
        获取数据
        :url_condition： 查询条件
        :return: 全部数据，以列表形式返回
        """
        # 获取url
        urls = self.mosaic_url()
        datas = [[
            "ID",
            "输入产品名称", "输入职业名称",
            "行业推荐1代码", "行业推荐1名称",
            "行业推荐2代码", "行业推荐2名称",
            "行业推荐3代码", "行业推荐3名称",
            "行业推荐4代码", "行业推荐4名称",
            "行业推荐5代码", "行业推荐5名称",
            "职业推荐1代码", "职业推荐1名称",
            "职业推荐2代码", "职业推荐2名称",
            "职业推荐3代码", "职业推荐3名称",
            "职业推荐4代码", "职业推荐4名称",
            "职业推荐5代码", "职业推荐5名称",
        ]]
        i = 1
        # 根据url获取返回值
        for url in urls:
            industry_datas = [{}, {}, {}, {}, {}]
            career_datas = [{}, {}, {}, {}, {}]
            if url.get("industry_condition") or url.get("career_condition"):
                data = []
                try:
                    if url.get('industry'):
                        industry_datas = json.loads(
                            requests.get(url.get('industry')).text.replace("(", "").replace(")", "").replace(";",
                                                                                                             "")).get(
                            "Codes")
                    if url.get("career"):
                        career_datas = json.loads(
                            requests.get(url.get('career')).text.replace("(", "").replace(")", "").replace(";",
                                                                                                           "")).get(
                            "Codes")

                    data.append(
                        url.get("ID"),
                    )
                    data.append(
                        url.get("industry_condition"),
                    )
                    data.append(
                        url.get("career_condition"),
                    )
                    for industry_data in industry_datas:
                        data.append(industry_data.get("Code"))
                        data.append(industry_data.get("Name"))
                    for career_data in career_datas:
                        data.append(career_data.get("Code"))
                        data.append(career_data.get("Name"))
                except:
                    data.append(
                        url.get("ID"),
                    )
                    data.append(
                        "连接报错",
                    )
                    data.append(
                        "连接报错",
                    )
                    for industry_data in industry_datas:
                        data.append(industry_data.get("Code"))
                        data.append(industry_data.get("Name"))
                    for career_data in career_datas:
                        data.append(career_data.get("Code"))
                        data.append(career_data.get("Name"))
                datas.append(data)
                self.progressBarValue.emit((i * 60 / len(urls)))  # 发送进度条的值 信号
            i += 1
        # 组装成list
        return datas

    def mosaic_url(self):
        """
        根据输入项进行url拼接
        :return:
        """
        urls = []
        for condition in self.url_condition:
            url = {
                "ID": condition.get("ID"),
                "industry": f"http://{self.url}/api/sic20172/querybydes?des={condition.get('search_industry')}" if condition.get(
                    'search_industry') else "",
                "industry_condition": condition.get('search_industry'),
                "career": f"http://{self.url}/api/occ2015/querybydes?des={condition.get('search_career')}" if condition.get(
                    'search_career') else "",
                "career_condition": condition.get('search_career'),
            }
            urls.append(url)
        return urls
